
import { ChannelID, ResultCallback, ResultState, compareVersion, BattleName } from "../SDKConfig";
import WXBannerAd from "./WXBannerAd";
import WXInterstitialAd from "./WXInterstitialAd";
import WXRewardedVideoAd from "./WXRewardedVideoAd";
import WXShare from "./WXShare";
import WXLogin from "./WXlogin";
import WXScreenshot from "./WXScreenshot";
import User from "../../game/db/User";
import ZSChannel from "../base/ZSChannel";
// import zsSdk = require("../../lib/zs.sdk.js");
import BigExportC from "../appBoxAd/BigExportC";
import OVNativeAdC from "../../game/nativeAd/OVNativeAdC";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import WXFileSystem from "./WXFileSystem";
import EventCount from "../base/EventCount";
// import ALDEventCount from "./ALDEventCount";
import CrazyClickC from "../../game/crazyClick/CrazyClickC";
import WXCustomAd from "./WXCustomAd";
export default class WXChannel extends ZSChannel {


    // protected eventCounter: EventCount
    constructor(id: ChannelID) {
        super(id)

        wx.onShow(() => {
            console.log('VivoChannel  onShow ')
            GlobalEvent.instance().emit(GlobalEvent.EVENT_SHOW)

        })

        wx.onHide(() => {
            console.log('VivoChannel  onHide ')
            GlobalEvent.instance().emit(GlobalEvent.EVENT_HIDE)

        })
        if (wx.createBannerAd) {
            this.bannerAd = new WXBannerAd(this)
        }

        if (wx.createInterstitialAd) {
            this.insertAd = new WXInterstitialAd(this)
        }

        if (wx.createRewardedVideoAd) {
            this.rewardAd = new WXRewardedVideoAd(this)
        }

        if (wx.shareAppMessage) {
            this.share = new WXShare(this)
        }

        if (wx.createInterstitialAd) {
            this.insertAd = new WXInterstitialAd(this)
        }

        if (wx.createCustomAd) {
            this.customAd = new WXCustomAd(this)
            // this.customAd.load()
        }

        this.loginMgr = new WXLogin(this)

        this.screenshot = new WXScreenshot(this)

        this.fileSystem = new WXFileSystem()
        this.checkForUpdate()
        // this.eventCounter = new ALDEventCount()

    }

    loginZS() {
        // zsSdk.login((openid) => {
        //     this.loadZSConfig()
        //     console.log(' msg ', openid)
        //     this.openID = openid

        //     zsSdk.init(this.openID)
        //     console.log('load ad start === ')
        //     this.loadAd()
        // }, (err) => {
        //     console.log('login error ', err)
        //     setTimeout(() => {
        //         this.loginZS()
        //     }, 3000);
        // })
    }
    checkForUpdate() {
        if (wx.getUpdateManager) {
            const updateManager = wx.getUpdateManager()

            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                console.log(res.hasUpdate)
            })

            updateManager.onUpdateReady(function () {
                wx.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，是否重启应用？',
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate()
                        }
                    }
                })
            })

            updateManager.onUpdateFailed(() => {
                // 新版本下载失败
                this.showToast('新版本下载失败,下次启动继续...')
            })
        }
    }


    vibrateShort() {
        wx.vibrateShort();
    }

    //展示网络图片
    previewImage(imgUrl: string) {
        wx.previewImage({
            current: imgUrl, // 当前显示图片的http链接
            urls: [imgUrl] // 需要预览的图片http链接列表
        })
    }

    //跳转能力
    navigateToMiniProgram(appID: string) {
        wx.navigateToMiniProgram({
            appId: appID,
            success: () => {

            }
        })
    }
    showToast(title: string) {
        wx.showToast({ title: title })
    }
    postMessage(msg: any) {
        let context = wx.getOpenDataContext()
        if (context) {
            msg.channelID = this.channelID;
            context.postMessage(msg)
        }
    }


    onCheckForUpdate(callback: ResultCallback) {

        callback(ResultState.YES)
        wx.getUpdateManager().onCheckForUpdate()

    }

    // hasMoreGame() {
    //     const version = wx.getSystemInfoSync().SDKVersion ;
    //     return compareVersion(version,'2.9.2') > 0
    // }

    hasCrazyClick() {
        return true
    }

    hasAppBox() {
        return true
    }

    showAppBoxAd(index: number) {
        // let list = this.getAdData()
        // if (list.length > 0) {
        BigExportC.instance().intoExport1(() => {
            BigExportC.instance().intoExport3()
        })
        // }

    }

    destroyAppBoxAd() {

    }

    hasBigExport() {
        return true;
    }
    openNativeView() {
        BigExportC.instance().intoExport1(() => {
            CrazyClickC.instance().gotoLayer(() => {
                BigExportC.instance().intoExport3()
            })

        })
    }

    protected battleMap: number[] = []

    battleEnd(id) {
        if (this.battleMap[id] == 1) {
            let name = BattleName[id]
            // console.log(' battleEnd ==  ',name )
            // this.eventCounter.battleEnd(id, name)
            this.battleMap[id] = 0;
        }
    }

    battleStart(id) {
        this.battleMap[id] = 1;
        let name = BattleName[id]
        // console.log(' battleStart ==  ',name )
        // this.eventCounter.battleStart(id, name)
    }
}
